<?php
class QuestionAction extends HomeCommonAction {

    public function index() {
        $condition = array();
        if(!empty($_POST['content'])) {
//             $_POST['content'] = iconv('GB2312', 'UTF-8',$_POST['content'] );
            $condition ['content'] = array('like',"%".$_POST['content']."%");
        }
        $Question = D('Question');
        import("ORG.Util.Page"); // 导入分页类
        $count      = $Question->where($condition)->count(); // 查询满足要求的总记录数
//        dump($Question->getlastsql());
        $Page       = new Page($count,3); // 实例化分页类传入总记录数和每页显示的记录数
        $show       = $Page->show(); // 分页显示输出
        $list = $Question->relation(true)->where($condition)->order('time_add desc')->limit($Page->firstRow.','.$Page->listRows)->select();
        //        print_r($list);
        $this->assign("_search",$_POST);
        $this->assign('list',$list);
        $this->assign('page', $show);
        $this->display('question_list');
    }

    public function show() {
        import("ORG.Util.String");
        $id = $_GET['id'];
        $Question = D('Question');
        $QusertionShare = D('QuestionShare');
        $question_info = $Question->relation(true) ->where("id = '{$id}'")->find();



        if(count($question_info) <= 0) {
            return false;
        }
        //补充分享
        $QuestionAdd = D('QuestionAdd');
        $addedList = $QuestionAdd->where('question_id = '.$id)->select();

        import("ORG.Util.Page"); // 导入分页类
        $count      = $QusertionShare->join('INNER JOIN `cm_share` as `share` ON share.id = cm_question_share.share_id  INNER JOIN `cm_user` as `user` on share.userid = user.id')->where("cm_question_share.question_id='{$id}'")->count(); // 查询满足要求的总记录数
        $Page       = new Page($count,10); // 实例化分页类传入总记录数和每页显示的记录数
        $show       = $Page->show(); // 分页显示输出

        $share_list = $QusertionShare->join(' INNER JOIN `cm_share` as `share`  ON share.id = cm_question_share.share_id INNER JOIN `cm_user` as `user` on share.userid = user.id')->where("cm_question_share.question_id='{$id}'")->order('cm_question_share.ins_time desc')->limit($Page->firstRow.','.$Page->listRows)->select();

        $Brand = D('Brand');

        for($i=0;$i<count($share_list);$i++) {
            $share_list[$i]['brand_count'] = $Brand -> where("share_id = '{$share_list[$i]['id']}'")->count();
        }
        for($i=0;$i<count($share_list);$i++){
            $share_list[$i]['content'] = String::msubstr(strip_tags($share_list[$i]['content']), $start=0, 250);
        }
//                        print_r($share_list);

        $best_count = $QusertionShare->where("question_id = '{$id}' and is_best_answer = 1")->count();
        //echo Session::getLocal('id');
        $Question->setInc('count_view',"id = '{$id}'",1);
        $this->assign('uid',Session::getLocal('id'));
        $this->assign('page',$show);
        $this->assign('count',$count);
        $this->assign('best_count',$best_count);
        $this->assign('question',$question_info);
        $this->assign('list',$share_list);
        $this->assign('addedlist',$addedList);
        $this->display('question');



    }



    public function showAdd() {
        $this->checkLogin();
        $this->display('person_question');
    }

    public function add() {
        $this->checkLogin();
        Load('extend');
        $data = array();
        //$data['title'] = $_POST['title'];
        //        echo $_POST['content'];
        $data['content'] = htmlspecialchars(trim($_POST['content']));
        $data['userid'] = Session::getLocal('id');
        $data['ip'] = get_client_ip();
        $data['time_add'] = time();
        $data['time_update'] = time();


        //        print_r($data);die;
        if(empty($data['content'])) {
            return false;
        }

        $Question = D('Question');
        $rs = $Question->data($data)->add();
        //         echo __APP__."Home/Question/show?id={$rs}";die;
        if($rs) {
            redirect(__APP__.'/Home/Question/show/id/'.$rs);
        }
        else {
            return false;
        }
    }
    public function questionAdded() {
        $this->checkLogin();
        $Question = D('Question');
        $question_info = $Question->where("id = '{$_POST['question_id']}'")->find();
        $uid = Session::getLocal('id');
        
        if($question_info['userid']!=$uid){
            $this->error("您没有权限补充该提问");
        }

        Load('extend');
        $data = array();
        $data['question_id'] = $_POST['question_id'];
        //        echo $_POST['content'];
        $data['content'] = htmlspecialchars(trim($_POST['content']));
        $data['userid'] = Session::getLocal('id');       
        $data['time_add'] = time();
        $data['time_update'] = time();


        //        print_r($data);die;
        if(empty($data['content'])) {
            return false;
        }

        $QuestionAdd = D('QuestionAdd');
        $rs = $QuestionAdd->data($data)->add();
        //         echo __APP__."Home/Question/show?id={$rs}";die;
        if($rs) {
            redirect(__APP__.'/Home/Question/show/id/'.$_POST['question_id']);
        }
        else {
            return false;
        }
    }
    public function addShare() {
        $this->checkLogin();
        $question_id = $_GET['question_id'];
        $share_id = $_GET['share_id'];

        if(intval($question_id) <= 0 || intval($share_id) <= 0) { echo 0; return false; }

        //验证提问是否存在
        $Question = D('Question');
        $question_info = $Question->where("id = '{$question_id}'")->find();
        if(count($question_info)<=0) {echo 0;return false;}

        //验证分享是否存在
        $Share = D('Share');
        $share_info = $Share->where("id = '{$share_id}'")->find();
        if(count($share_info)<=0) {echo 0;return false;}

        //查询该分享是否已关联到该提问
        $QusertionShare = D('QuestionShare');
        $myshare = $QusertionShare->where("share_id = '{$share_id}' and question_id = '{$question_id}'")->find();
        if(count($myshare)>0) { echo 2; return false; }

        //入库
        $data = array();
        $data['question_id'] = $question_id;
        $data['share_id'] = $share_id;
        $data['ins_time'] = time();
        $rs=$QusertionShare->data($data)->add();
        if(!$rs) { echo 0; return false; }
        $Question->setInc('count_share',"id = '{$question_id}'" ,1);
        echo 1;  return true;
    }

    public function best() {
        $this->checkLogin();
        $question_id = $_GET['question_id'];
        $share_id = $_GET['share_id'];
        //验证提问是否存在
        $Question = D('Question');
        $question_info = $Question->where("id = '{$question_id}'")->find();
        if(count($question_info)<=0) {echo 0;return false;}

        //验证分享是否存在
        $Share = D('Share');
        $share_info = $Share->where("id = '{$share_id}'")->find();
        if(count($share_info)<=0) {echo 0;return false;}
        //查询该分享是否已关联到该提问
        $QusertionShare = D('QuestionShare');
        $myshare = $QusertionShare->where("share_id = '{$share_id}' and question_id = '{$question_id}'")->find();
        if(count($myshare)<=0) {echo 0;return false;}
        $time = time();
        $rs = $QusertionShare->where("question_id = '{$question_id}' and share_id = '{$share_id}'")->save(array('is_best_answer'=>1,'ins_time'=>$time));
        //        $this->display('debug');
        if($rs) {
            echo 1;return true;
        }
        echo 0;return false;

    }

    public function del() {
        $this->checkLogin();
        $Question = D('Question');
        $id = $_GET['id'];
        if(intval($id)<=0) {
            echo 0;
        }
        else {
            $rs = $Question->where('id = '.$id)->save(array('is_del'=>'1'));
            if($rs) {
                echo 1;
            }
        }
        return;
    }

    public function top() {
        $this->checkLogin();
        $Question = D('Question');
        $id = $_GET['id'];
        if(intval($id)<=0) {
            echo 0;
        }
        else {
            $time = time();
            $rs = $Question->where('id = '.$id)->save(array('time_add'=>$time));
            if($rs) {
                echo 1;
            }
        }
        return;
    }
}
?>
